﻿window.mgpTpl={
	'reg':new RegExp(/<%(.*?)%>/g),
	'compile':function(tmpl,obj){
		var tmpl=tmpl.replace(/"/g,'\\"');
		var tmpl=tmpl.replace(/'/g,'\\\'');
		var reg0=this.reg;var count=0;
		var a;var lastIndex=reg0.lastIndex;var index=0;
		var script='(function(){var __tmplRes__ = "";\nvar __this__=obj;\n';
		for(var key in obj){
			script+='var '+key+'='+'__this__["'+key+'"];\n';
		}
		while((a=reg0.exec(tmpl)) != null){
			count++;
			if(count>10000){
				console.log('老铁循环超过一万了');break;
			}
			index=a.index;
			script+=this.str2script(tmpl.substring(lastIndex,index));
			var tmplStr=(a[1]);
			if(tmplStr.startsWith('=')){
				script+=this.str2script2(tmplStr.substring(1))+'\n';
			}else{
				script+=tmplStr+'\n';
			}
			lastIndex=reg0.lastIndex;
		}
		script+=this.str2script(tmpl.substring(lastIndex,tmpl.length));
		script+='return __tmplRes__;})();';
		var res=eval(script)
		return res;
	},
	'str2script':function(str){
		return '__tmplRes__+="'+str+'";\n';
	},
	'str2script2':function (str){
		return '__tmplRes__+='+str+';\n';
	}
}